package com.tanhua.server.controller;


import com.itheima.model.vo.PageResult;
import com.itheima.model.vo.SettingsVo;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import com.tanhua.template.OssTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;


    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private UserService userService;

    /**
     * 显示通用设置
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity findUserSettings() {

        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);

    }

    /**
     * 设置陌生人问题
     *
     * @param map
     * @return
     */

    @PostMapping("/questions")
    public ResponseEntity findQuestion(@RequestBody Map map) {
        //获取参数
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map) {
        //获取参数
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单列表
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        //1、调用service查询
        PageResult pr = settingsService.blacklist(page, size);
        //2、构造返回
        return ResponseEntity.ok(pr);
    }

    /**
     * 取消黑名单
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 发送手机验证码,修改手机号
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //发送验证码
        settingsService.sendVerification();
        return ResponseEntity.ok("设置成功");
    }

    /**
     * 修改手机改号校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> map) {
        String verificationCode = map.get("verificationCode");
        Boolean verification = settingsService.checkVerification(verificationCode);
        HashMap<String, Boolean> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("verification", verification);
        return ResponseEntity.ok(objectObjectHashMap);
    }

    /**
     *修改手机号
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map<String, String> map){
        String phone = map.get("phone");
        settingsService.phone(phone);
        return ResponseEntity.ok("修改成功!");
    }
}